package cn.netty.exemple7.protobuf;

import com.google.protobuf.InvalidProtocolBufferException;

public class SubscribeReqProtoTest {

    private static byte[] encode(SubscribeReqProto.SubscribeReq req){
        return req.toByteArray();
    }

    private static SubscribeReqProto.SubscribeReq decode(byte[] body) throws InvalidProtocolBufferException {
        return SubscribeReqProto.SubscribeReq.parseFrom(body);
    }

    private static SubscribeReqProto.SubscribeReq createSubscribeReq(){
        SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();

        builder.setId(1);
        builder.setAddress("广州天河区");
        builder.setUserName("chenzhipeng");
        builder.setPreductName("netty in action");
        return builder.build();
    }

    public static void main(String[] args) throws InvalidProtocolBufferException {
        SubscribeReqProto.SubscribeReq subscribeReq = createSubscribeReq();
        System.out.println("Before encode:"+subscribeReq.toString());


        SubscribeReqProto.SubscribeReq decode = decode(encode(subscribeReq));

        System.out.println("After decode:"+decode.toString());
        System.out.println("After equal:"+subscribeReq.equals(decode));


    }
}
